package com.app.newcsc.fragment.my.set;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;

import com.app.newcsc.BR;
import com.app.newcsc.R;
import com.app.newcsc.activity.SplashActivity;
import com.app.newcsc.app.AppViewModelFactory;
import com.app.newcsc.app.Constant;
import com.app.newcsc.data.RetrofitClient;
import com.app.newcsc.databinding.FragmentSelectLineBinding;
import com.app.newcsc.entity.ConfigEntity;
import com.donkingliang.labels.LabelsView;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import me.goldze.mvvmhabit.base.AppManager;
import me.goldze.mvvmhabit.base.BaseFragment;

/**
 * @author: woking
 * @date: 2021/6/11
 * @description：
 */
public class SelectLineFragment extends BaseFragment<FragmentSelectLineBinding, SetViewModel> {
    private List<String> line_urls = new ArrayList<>();
    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_select_line;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }
    @Override
    public SetViewModel initViewModel() {
        //使用自定义的ViewModelFactory来创建ViewModel，如果不重写该方法，则默认会调用NetWorkViewModel(@NonNull Application application)构造方法
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getActivity().getApplication());
        return ViewModelProviders.of(this, factory).get(SetViewModel.class);
    }
    @Override
    public void initData() {
        super.initData();
        binding.ivTitleBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getActivity().finish();
            }
        });
        binding.tvLine1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(line_urls.size() > 0)
                {
                    Random random = new Random();
                    int index = random.nextInt(line_urls.size());
                    Log.e("TAG","==========random======1======" + index);
                    RetrofitClient.baseUrl = line_urls.get(index)+"/";
                }
                
                finishAllActivity();
            }
        });
        binding.tvLine2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(line_urls.size() > 0)
                {
                    Random random = new Random();
                    int index = random.nextInt(line_urls.size());
                    Log.e("TAG","==========random======2======" + index);
                    RetrofitClient.baseUrl = line_urls.get(index)+"/";
                }
                finishAllActivity();
            }
        });
        viewModel.mConfigEntity.observe(this, new Observer<ConfigEntity>() {
            @Override
            public void onChanged(ConfigEntity configEntity) {
                try {
                    if(configEntity != null && configEntity.line_urls != null && configEntity.line_urls.size() > 0)
                    {
                        line_urls.clear();
//                        line_urls.addAll(configEntity.line_urls);
//                        line_urls.addAll(configEntity.line_urls);
                        line_urls.addAll(configEntity.line_urls);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        viewModel.apiConfigs();

        List<String > datas = new ArrayList<>();
        datas.add("线路一");
        datas.add("线路二");
        binding.tvItemSelectCommodityDatas.setSelectType(LabelsView.SelectType.SINGLE_IRREVOCABLY);
        binding.tvItemSelectCommodityDatas.setLabels(datas, (label, position, data) -> datas.get(position));
        binding.tvItemSelectCommodityDatas.setSelects(0);
        binding.tvItemSelectCommodityDatas.setOnLabelSelectChangeListener((label, data, isSelect, position) -> {
//            Log.e("TAG",data + "============isSelect============="+isSelect);
            if(isSelect)
            {
                Observable.interval(0, 1, TimeUnit.SECONDS)
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .map(aLong -> Constant.SECOND_COUNT - aLong.intValue())
                        .take(1)
                        .subscribe(new io.reactivex.Observer<Integer>() {
                            @Override
                            public void onSubscribe(Disposable d) {
                            }
                            @Override
                            public void onNext(Integer integer) {
                            }
                            @Override
                            public void onError(Throwable e) {
                            }
                            @Override
                            public void onComplete() {
                                if(line_urls.size() > 0)
                                {
                                    Random random = new Random();
                                    int index = random.nextInt(line_urls.size());
                                    Log.e("TAG","==========random======2======" + index);
                                    RetrofitClient.baseUrl = line_urls.get(index)+"/";
                                }
                                finishAllActivity();
                            }
                        });
            }
        });
    }

    private void finishAllActivity() {
        AppManager.getAppManager().finishAllActivity();
        Intent intent = new Intent(getContext(), SplashActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(intent);

        getActivity().finish();
    }
}